//
//  SLSettingsViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/20.
//


#import "SLSettingsViewController.h"
#import <YYKit.h>
#import <Masonry.h>


@interface SLSettingsTableViewCell : UITableViewCell
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *arrow;
@end
@implementation SLSettingsTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.icon];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.arrow];
        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).inset(14);
            make.centerY.equalTo(self.contentView);
            make.width.height.mas_equalTo(29);
        }];
        [self.titleLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.icon.mas_right).offset(7);
            make.centerY.equalTo(self.icon);
        }];
        [self.arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).inset(14);
            make.left.equalTo(self.titleLabel.mas_right);
            make.centerY.equalTo(self.contentView);
            make.width.height.mas_equalTo(22);
        }];
    }
    return self;
}
- (UIImageView *)icon {
    if (!_icon) {
        _icon = [UIImageView new];
        _icon.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _icon;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = aspectDefaultFont(14);
        _titleLabel.textColor = UIColorHex(#666666);
    }
    return _titleLabel;
}
- (UIImageView *)arrow {
    if (!_arrow) {
        _arrow = [UIImageView new];
        _arrow.contentMode = UIViewContentModeScaleAspectFit;
        _arrow.image = [UIImage imageNamed:@"btn_more_pink"];
    }
    return _arrow;
}
@end


@interface SLSettingsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIImageView *decorate;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *circlebar;
@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation SLSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavBar:@"Settings"];
    self.data = @[
        @{
            @"image": @"icon_mine_loan_agreement",
            @"title": @"Loan Agreement",
        },
        @{
            @"image": @"icon_mine_order",
            @"title": @"Privacy Policy",
        },
    ].mutableCopy;
    
    self.view.backgroundColor = UIColorHex(#F5F5F5);
    [self.view addSubview:self.decorate];
    [self.decorate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).inset(-40);
        make.height.mas_equalTo(440);
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(20);
        //make.top.equalTo(self.view).inset(10);
        make.top.equalTo(self.navBar.mas_bottom).offset(20);
        make.bottom.equalTo(self.view).inset(40);
    }];
    [self.view addSubview:self.circlebar];
    [self.circlebar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.tableView);
        make.top.equalTo(self.tableView.mas_top);
        make.height.mas_equalTo(25);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SLSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLSettingsTableViewCell" forIndexPath:indexPath];
    NSDictionary *dict = self.data[indexPath.row];
    if ([dict isKindOfClass:NSDictionary.class]) {
        cell.icon.image = [UIImage imageNamed:dict[@"image"]];
        cell.titleLabel.text = dict[@"title"];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return aspectValue(59);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = self.data[indexPath.row];
    if (![dict isKindOfClass:NSDictionary.class]) {
        return;
    }
    if ([dict[@"title"] isEqualToString:@"Loan Agreement"]) {
        [SLRoutePush route:Configuration.LoanAgreementDomain parameter:nil];
    } else if ([dict[@"title"] isEqualToString:@"Privacy Policy"]) {
        [SLRoutePush route:Configuration.PrivacyPolicyDomain parameter:nil];
    }
}

- (UIImageView *)decorate {
    if (!_decorate) {
        _decorate = [UIImageView new];
        _decorate.backgroundColor = UIColor.blackColor;
        _decorate.contentMode = UIViewContentModeScaleAspectFit;
        _decorate.layer.cornerRadius = 20;
    }
    return _decorate;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.layer.cornerRadius = 20;
        _tableView.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _tableView.layer.borderWidth = 1.5;
        _tableView.contentInset = UIEdgeInsetsMake(30, 0, 0, 0);
        [_tableView registerClass:SLSettingsTableViewCell.class forCellReuseIdentifier:@"SLSettingsTableViewCell"];
        _tableView.tableFooterView = [self tableFooterView];
    }
    return _tableView;
}
- (UIImageView *)circlebar {
    if (!_circlebar) {
        _circlebar = [UIImageView new];
        _circlebar.contentMode = UIViewContentModeScaleAspectFit;
        _circlebar.image = [UIImage imageNamed:@"icon_circle_bar"];
    }
    return _circlebar;
}
- (UIView *)tableFooterView {
    UIView *foot = [UIView new];
    foot.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 145);
    SLMoreButton *terminate = [SLMoreButton new];
    terminate.titleLabel.text = @"Terminate The Account";
    terminate.titleLabel.textColor = UIColorHex(#1B1B1B);
    terminate.imageView.image = [UIImage imageNamed:@"btn_more_black"];
    terminate.layer.cornerRadius = 24;
    terminate.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
    terminate.layer.borderWidth = 1.5;
    terminate.titleLabel.font = aspectDefaultFont(16);
    [foot addSubview:terminate];
    [terminate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(foot);
        make.top.equalTo(foot).offset(40);
        make.height.mas_equalTo(54);
        make.width.mas_equalTo(terminate.intrinsicContentSize.width+30*2);
    }];
    [terminate jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [SLAlertView terminateAccountAlertWithLeftCallback:^(UIButton * _Nonnull button) {
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [TerminateRequestProvider requestTerminateWithSuccess:^(BOOL success) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                [SLRoutePush rootRoute:@"SLLoginViewController"];
            } failure:^(YTKRequest * _Nonnull request) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                [ResponseToast showToastWithRequest:request];
            }];
        } rightCallback:^(UIButton * _Nonnull button) {
            
        }];
    }];
    
    UILabel *update = [UILabel new];
    update.text = @"Check For Updates";
    update.font = aspectDefaultFont(14);
    update.userInteractionEnabled = YES;
    [foot addSubview:update];
    [update mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(terminate.mas_bottom).offset(0);
        make.centerX.equalTo(foot);
        make.height.mas_equalTo(38);
    }];
    @weakify(self);
    [update jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self);
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.view makeToast:@"The current version is up to date."];
        });
    }];
    
    return foot;
}
@end
